package com.example.football;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.football.entity.Schedule;
import com.example.football.util.JDBCUtils;
import com.example.football.util.SetTeamlogo;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class HisPeopleScheduleActivity extends AppCompatActivity {
    ListView myList;
    static public Integer peopleid ;
    static public String account;
    static public String name;
    static public ArrayList<Schedule> list;

    SimpleDateFormat formatter;
    Date curDate;
    String   currentTime;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.history_peopleschedule);

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }

        peopleid = Main2Activity.peopleid;
        account = Main2Activity.account;
        name = Main2Activity.name;
        list = new ArrayList<>();

        formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        curDate = new Date(System.currentTimeMillis());
        currentTime = formatter.format(curDate);

        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("id",peopleid);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String url="http://"+ JDBCUtils.ip +":9000/APPpeopleschedule";
        RequestQueue requestQueue= Volley.newRequestQueue(HisPeopleScheduleActivity.this);
        JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.POST, url,jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                try {
                    Log.d("信息", jsonObject.toString());
                    String msg = jsonObject.getString("msg");
                    Log.d("msg", msg);

                    JSONArray detail = jsonObject.getJSONArray("detail");
                    for(int i = 0; i < detail.length(); i++){
                        JSONObject data = detail.getJSONObject(i);
                        String s = data.getString("time").substring(0,10)+" "+data.getString("time").substring(11,19);
                        if(currentTime.compareTo(s)>0) {
                            list.add(new Schedule(data.getInt("id"), data.getString("hometeam"), data.getString("guestteam"),
                                    s, data.getString("platform"), data.getString("league")));
                        }
                    }
                    myList = findViewById(R.id.mylist6);
                    final MyAdapter myAdapter = new MyAdapter();
                    myList.setAdapter(myAdapter);
                    myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                            String str=list.get(position).getHometeam();
                            Integer scheduleid = list.get(position).getId();
                            Log.d("检查：","OnItemClick功能实现！\n"+str+scheduleid);
                        }
                    });

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(HisPeopleScheduleActivity.this, "网络出错", Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(jsonObjectRequest);
    }

    public class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {//获取item的数量
            return list.size();
        }

        @Override
        public Object getItem(int i) {//i表示当前item的序号，序号从0开始编写，getIte用于获取某个item
            return list.get(i);
        }

        @Override
        public long getItemId(int i) {//获取选中的item的编号
            return i;
        }

        @Override//用于配置listview要加载的内容，包括视图和数据
        public View getView(final int i, View view, ViewGroup viewGroup) {

//             优化写法
            ViewHolder holder = null;
            if(view == null){
                //这里是将item视图与view绑定起来
                view = View.inflate(HisPeopleScheduleActivity.this,R.layout.peopleschedule_item,null);
                holder = new ViewHolder();
//                注意，这里是去view的视图里面拿id
                holder.hometeam = view.findViewById(R.id.hometeam1);
                holder.guestteam = view.findViewById(R.id.guestteam1);
                holder.time = view.findViewById(R.id.time1);
                holder.league = view.findViewById(R.id.league1);
                holder.platform = view.findViewById(R.id.platform1);
                holder.hometeamlogo = view.findViewById(R.id.hometeamlogo1);
                holder.guestteamlogo = view.findViewById(R.id.guestteamlogo1);
                holder.yuding = view.findViewById(R.id.yuding1);

                holder.hometeam.setText(list.get(i).getHometeam());
//                holder.iv.setBackgroundResource(foods.get(i).getIcon());
                holder.guestteam.setText(list.get(i).getGuestteam());
                holder.time.setText(list.get(i).getTime());
                holder.league.setText(list.get(i).getLeague());
                holder.platform.setText(list.get(i).getPlatform());
                SetTeamlogo.setTeamlogo(holder.hometeamlogo,list.get(i).getHometeam());
                SetTeamlogo.setTeamlogo(holder.guestteamlogo,list.get(i).getGuestteam());

                view.setTag(holder);
            }else {
                holder = (ViewHolder) view.getTag();
                holder.hometeam.setText(list.get(i).getHometeam());
//                holder.iv.setBackgroundResource(foods.get(i).getIcon());
                holder.guestteam.setText(list.get(i).getGuestteam());
                holder.time.setText(list.get(i).getTime());
                holder.league.setText(list.get(i).getLeague());
                holder.platform.setText(list.get(i).getPlatform());
                SetTeamlogo.setTeamlogo(holder.hometeamlogo,list.get(i).getHometeam());
                SetTeamlogo.setTeamlogo(holder.guestteamlogo,list.get(i).getGuestteam());
            }

            return view;
        }

    }



    static class ViewHolder{
        TextView time;
        TextView league;
        TextView hometeam;
        TextView guestteam;
        TextView platform;
        ImageView hometeamlogo;
        ImageView guestteamlogo;
        ImageView yuding;
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.historypeopleschedule_iv_back:
                finish();
                break;
        }
    }
}
